package com.it.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.bean.Swiper;
import com.it.mapper.SwiperMapper;
import com.it.page.PageQuery;
import com.it.service.SwiperService;
import com.it.utils.AppUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service("swiperService")
@RequiredArgsConstructor
public class SwiperServiceImpl implements SwiperService {

    private final SwiperMapper swiperMapper;

    @Override
    public PageInfo<Swiper> page(PageQuery<Swiper> pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());
        Swiper condition = pageQuery.getQueryBody();
        condition.setDeleted(false);
        List<Swiper> ds = swiperMapper.select(condition);
        return new PageInfo<>(ds);
    }

    @Override
    public void delete(Long id) {
        Swiper swiper = swiperMapper.selectByPrimaryKey(id);
        swiper.setDeleted(true);
        AppUtils.forUpdateAudit(swiper);
        swiperMapper.updateByPrimaryKeySelective(swiper);
    }

    @Override
    public void save(Swiper swiper) {
        if (swiper.getId() == null) {
            AppUtils.forSaveAudit(swiper);
            swiperMapper.save(swiper);
        } else {
            AppUtils.forUpdateAudit(swiper);
            swiperMapper.updateByPrimaryKeySelective(swiper);
        }
    }
}
